package com.swpu.project.model.entity;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;


@Data
@NoArgsConstructor
@AllArgsConstructor
public class LoginUser implements UserDetails {

    private User user;
     private List<String> premissions;
     private List<String> blockNums;


     /*
     *  为了对区块权限进行细粒度控制的同时提升效率,如果权限完全匹配为真,或者检测到区块通配符*后续不进行区块校验(针对系统管理员、全区数据操作人员权限)
     *
     *
     * */
     Boolean judge=false;
     @JSONField(serialize = false)
     private  List<SimpleGrantedAuthority> authorities;


    public LoginUser(User user, List<String> list) {
     this.user=user;
     this.premissions=list;
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(authorities!=null)
            return authorities;

      List<SimpleGrantedAuthority> collect = premissions.stream().map(s -> new SimpleGrantedAuthority(s)).collect(Collectors.toList());
      authorities=collect;

        return authorities;


    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}